<?php
/* 
* @Title:  [商品类型模型]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-04 21:10:12
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-11 21:09:00
* @Copyright:  [hn7m.com]
*/
class TypeModel extends Model{
	public $table="type";

	/**
	 * [$validate 验证规则]
	 * @var array
	 */
	public $validate=array(
		array('ttitle','nonull','类型名称不能为空',2,3)
		);

	/**
	 * [add_type_data 添加类型]
	 */
	public function add_type_data(){
		$attr=Q('post.ttitle');
		$preg="/[\s,\r\n]+/is";
		if (!preg_match($preg, $attr)) {
			$this->error='格式不正确,必须以","或者"换行"结束';
			return false;
		}

		$arr=preg_split($preg, $attr);
		//去重
		$arr=array_unique($arr);

		//循环插入
		foreach ($arr as $key => $value) {
			if(empty($value)) continue;
			$data=array('ttitle'=>$value);
			$this->add($data);
		}
		return true;
	}
	/**
	 * [get_data 获得所有的类型,并按主键排序]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function get_data($where=NULL){
		return $this->order('tid asc')->where($where)->findAll();
	}
	/**
	 * [get_onedate_value 获得某个字段记录值]
	 * @param  [type] $tid  [description]
	 * @param  [type] $need [需要的字段]
	 * @return [type]       [description]
	 */
	public function get_onedate_value($tid,$need){
		return $this->where(array('tid'=>$tid))->getField($need);
	}
}
?>